/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Products;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class ProductDAO extends DAO {

    public ProductDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Products> getAll() throws Exception {
        List<Products> a = (List<Products>) getSession().createQuery("from Products a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Products get(int id) throws Exception {
        Products a = (Products) getSession().createQuery("from Products a where a.productid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Products a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Products a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Products> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Products> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    @SuppressWarnings("unchecked")
    public List<Products> getActiveProducts() throws Exception {
        //int i = 1;
        List<Products> prods = (List<Products>) getSession().createQuery("from Products p where p.isactive=:status").setParameter("status", true).setCacheable(true).list();
        flush();
        return prods;
    }

    @SuppressWarnings("unchecked")
    public Products getByProdName(int pname) throws Exception {
        Products prods = (Products) getSession().createQuery("from Products p where p.productname=:pname").setParameter("pname", pname).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return prods;
    }
    
    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(int prodname) throws Exception {
        Products p = (Products) getSession().createQuery("from Products p where p.productname=:prodname").setParameter("prodname", prodname).setCacheable(true).uniqueResult();
        flush();
        if (p == null) {
            return false;
        }
        return true;
    }
    
}
